package cmbc.honorcredit.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class JasonUtil {
	
	public static String createJsonString(Object value)
    {
        Gson gson = new Gson();
        String str = gson.toJson(value);
        return str;
    }

	public static <T> T getObject(String jsonString, Class<T> cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString, cls);
        } catch (Exception e) {
            // TODO: handle exception
        	e.printStackTrace();
        }
        return t;
    }
	
	public static <T> List<T> getObjects(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
            }.getType());
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return list;
    }
	
	public static List<Map<String, Object>> listKeyMaps(String jsonString) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString,
                    new TypeToken<List<Map<String, Object>>>() {
                    }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
	
	
	public static void main(String[] args) throws Exception {
		
		String json = "{\"score\":120,\"reasons\":[{\"message\":\"Large number of applications in network\",\"code\":\"NUM_APPS\"},{\"message\":\"1 rejected applications on network\",\"code\":\"NUM_REJECTED\"}]}";
		
		IREResult result = getObject(json, IREResult.class);
		
		System.out.println(result.getScore());

		System.out.println(createJsonString(result));
		
		
	}
}
